//
//  NSDate+Extend.m
//  HelloWorld
//
//  Created by 1 on 16/7/4.
//  Copyright (c) 2016年 zhiyou. All rights reserved.
//

#import "NSDate+Extend.h"

@implementation NSDate (Extend)
- (NSInteger) year
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitYear fromDate:self];
    return [components year];
}

//获取当前的月
-(NSInteger)month
{
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitMonth fromDate:self];
    return [components month];
}

//获取当前的日
-(NSInteger)day {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitDay fromDate:self];
    return [components day];
}

-(NSInteger)hour1 {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitHour fromDate:self];
    return [components hour];
}

-(NSInteger)minute1 {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitMinute fromDate:self];
    return [components minute];
}

-(NSInteger)second1 {
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitSecond fromDate:self];
    return [components second];
}


- (NSInteger)weak{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags =NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    comps = [calendar components:unitFlags fromDate:self];

    return comps.weekday;

}
//获取当前月有多少天
- (NSUInteger)numberOfDaysInCurrentMonth
{
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self].length;
}

/**
 *  获取本月第一天的星期
 *
 */
- (NSInteger)weakForMonthFirstDay{
    NSInteger weak  = self.weak - self.day % 7 +1;
    if (weak < 1)
    {
        weak += 7;
    }
    if (weak>7) {
        weak=weak-7;
    }
    return weak;
}

//2.在调用方法的类中写方法

//上一个月的日期
- (NSDate *)lastMonthDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitWeekday | NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitDay) fromDate:date];
    if ([components month] == 1)
    {
        [components setMonth:12];
        [components setYear:[components year] - 1];
    }
    else
    {
        [components setMonth:[components month] - 1];
    }
    NSDate *lastMonth = [calendar dateFromComponents:components];
    return lastMonth;
}

//下一个月的日期
- (NSDate *)nextMonthDate:(NSDate *)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitWeekday|NSCalendarUnitMonth |NSCalendarUnitYear | NSCalendarUnitDay) fromDate:date];
    if ([components month] == 12)
    {
        [components setMonth:1];
        [components setYear:[components year] + 1];
    }
    else
    {
        [components setMonth:[components month] + 1];
    }
    NSDate *lastMonth = [calendar dateFromComponents:components];
    return lastMonth;
}




+(NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day
{
    
    NSDateComponents *components = [[NSDateComponents alloc]init];
    components.year = year;
    components.month = month;
    components.day = day;
    components.hour = 0;
    
    
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDate *date = [calender dateFromComponents:components];
    components.year = NSIntegerMax;
    components.month = NSIntegerMax;
    components.day = NSIntegerMax;
    components.hour = NSIntegerMax;    
    
    return date;
}

+(NSArray*)componentsSeparatedByDate:(NSDate*)date
{
    NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString*str=[formatter stringFromDate:date];
    
    NSArray*arr=[str componentsSeparatedByString:@"-"];
    
    return arr;
}



@end
